﻿using LiteFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Website.Action
{
    public abstract class BaseHttpHandler_ACE : BaseHttpHandler
    {
        protected override bool onDoAction(ref string action, out ActionStatus code, out string err)
        {
            code = ActionStatus.ok;
            err = null;
            if (action == "edit")
            {
                string oper = this.__context.Request.Form["oper"];
                if (oper == "del") action = "del";
                else if (oper == "add") action = "add";
            }

            return true;
        }
    }


    public interface IAceModel : LiteFramework.IJson { }
    public class ActionResult_AceList<T> : ActionResult, IJson
        where T : IAceModel
    {
        private List<T> _list = null;

        public ActionResult_AceList()
        {
            this.PageSize = 20;
            this.PageNum = 1;
            this.SordAsc = true;
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalRecords { get; set; }
        /// <summary>
        /// 总页数。自动根据TotalRecords & PageSize计算
        /// </summary>
        public int TotalPages { get { return this.PageSize > 0 ? this.TotalRecords / this.PageSize + (TotalRecords % this.PageSize > 0 ? 1 : 0) : this.TotalRecords > 0 ? 1 : 0; } }
        /// <summary>
        /// 每页数据量（默认20）
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 页码（从１开始）
        /// </summary>
        public int PageNum { get; set; }
        /// <summary>
        /// 是否正序排序
        /// </summary>
        public bool SordAsc { get; set; }
        public string sidx { get; set; }

        public ActionResult_AceList(IEnumerable<T> list)
        {
            if (list != null && list.Count() > 0)
            {
                this._list = new List<T>();
                this._list.AddRange(list);
            }
        }

        public void Add(T model)
        {
            if (model != null)
            {
                if (this._list == null) this._list = new List<T>();
                this._list.Add(model);
            }
        }

        public override string ToString()
        {
            return this.ToJsonString();
        }

        public string ToJsonString()
        {
            string data = string.Empty;
            if (this._list != null && this._list.Count > 0) data = string.Join(",", _list.Select(i => i.ToJsonString()));

            return @"{ "
                        + string.Format("\"ok\" : {0}", this.ok ? "true" : "false")
                        + string.Format(", \"status\" : {0}", (int)this.status)
                        + string.Format(", \"message\" : \"{0}\"", this.message)
                        + string.Format(", \"total\" : {0}", this.TotalPages)
                        + string.Format(", \"page\" : {0}", this.PageNum)
                        + string.Format(", \"records\" : {0}", this.TotalRecords)
                        + string.Format(", \"repeatitems\" : false")
                        + string.Format(", \"rows\" : [ {0} ]", data)
                        + " }";
        }
    }
}